﻿//using StudentManagementSystem.Models;
using StudentManagementSystem.Data;
using System;
using System.Linq;
using StudentManagementSystem;

class Program
{
    private static DatabaseHelper _dbHelper = new DatabaseHelper("Data Source=student.db");

    static void Main(string[] args)
    {
        Console.WriteLine("学生管理系统 v1.0");
        Console.WriteLine("----------------");

        // 初始化数据库
        _dbHelper.InitializeDatabase();

        while (true)
        {
            ShowMainMenu();
            var choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    ManageSchools();
                    break;
                case "2":
                    ManageClasses();
                    break;
                case "3":
                    ManageStudents();
                    break;
                case "4":
                    ViewLogs();
                    break;
                case "5":
                    Console.WriteLine("感谢使用，再见！");
                    return;
                default:
                    Console.WriteLine("无效选项，请重新输入");
                    break;
            }
        }
    }

    static void ShowMainMenu()
    {
        Console.WriteLine("\n主菜单");
        Console.WriteLine("1. 学校管理");
        Console.WriteLine("2. 班级管理");
        Console.WriteLine("3. 学生管理");
        Console.WriteLine("4. 查看操作日志");
        Console.WriteLine("5. 退出系统");
        Console.Write("请选择操作: ");
    }

    #region 学校管理
    static void ManageSchools()
    {
        while (true)
        {
            Console.WriteLine("\n学校管理");
            Console.WriteLine("1. 添加学校");
            Console.WriteLine("2. 查看所有学校");
            Console.WriteLine("3. 更新学校信息");
            Console.WriteLine("4. 删除学校");
            Console.WriteLine("5. 返回主菜单");
            Console.Write("请选择操作: ");

            var choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    AddSchool();
                    break;
                case "2":
                    ListAllSchools();
                    break;
                case "3":
                    UpdateSchool();
                    break;
                case "4":
                    DeleteSchool();
                    break;
                case "5":
                    return;
                default:
                    Console.WriteLine("无效选项");
                    break;
            }
        }
    }

    static void AddSchool()
    {
        Console.WriteLine("\n添加新学校");

        Console.Write("学校名称: ");
        var name = Console.ReadLine();

        Console.Write("学校地址: ");
        var address = Console.ReadLine();

        var school = new School { Name = name, Address = address };
        var id = _dbHelper.AddSchool(school);

        Console.WriteLine($"学校添加成功，ID: {id}");
    }

    static void ListAllSchools()
    {
        Console.WriteLine("\n学校列表:");
        Console.WriteLine("ID\t名称\t地址");
        Console.WriteLine("----------------------------");

        var schools = _dbHelper.GetAllSchools();
        foreach (var school in schools)
        {
            Console.WriteLine($"{school.Id}\t{school.Name}\t{school.Address}");
        }
    }

    static void UpdateSchool()
    {
        ListAllSchools();
        Console.Write("\n请输入要更新的学校ID: ");
        if (!int.TryParse(Console.ReadLine(), out int id))
        {
            Console.WriteLine("无效的ID");
            return;
        }

        var school = _dbHelper.GetSchoolById(id);
        if (school == null)
        {
            Console.WriteLine("未找到该学校");
            return;
        }

        Console.Write($"新名称(当前: {school.Name}): ");
        var name = Console.ReadLine();
        if (!string.IsNullOrWhiteSpace(name))
        {
            school.Name = name;
        }

        Console.Write($"新地址(当前: {school.Address}): ");
        var address = Console.ReadLine();
        school.Address = address;

        _dbHelper.UpdateSchool(school);
        Console.WriteLine("学校信息已更新");
    }

    static void DeleteSchool()
    {
        ListAllSchools();
        Console.Write("\n请输入要删除的学校ID: ");
        if (!int.TryParse(Console.ReadLine(), out int id))
        {
            Console.WriteLine("无效的ID");
            return;
        }

        Console.Write("确认删除该学校？(y/n): ");
        if (Console.ReadLine().ToLower() == "y")
        {
            _dbHelper.DeleteSchool(id);
            Console.WriteLine("学校已删除");
        }
    }
    #endregion

    #region 班级管理
    static void ManageClasses()
    {
        while (true)
        {
            Console.WriteLine("\n班级管理");
            Console.WriteLine("1. 添加班级");
            Console.WriteLine("2. 查看所有班级");
            Console.WriteLine("3. 更新班级信息");
            Console.WriteLine("4. 删除班级");
            Console.WriteLine("5. 返回主菜单");
            Console.Write("请选择操作: ");

            var choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    AddClass();
                    break;
                case "2":
                    ListAllClasses();
                    break;
                case "3":
                    UpdateClass();
                    break;
                case "4":
                    DeleteClass();
                    break;
                case "5":
                    return;
                default:
                    Console.WriteLine("无效选项");
                    break;
            }
        }
    }

    static void AddClass()
    {
        ListAllSchools();
        Console.Write("\n请输入所属学校ID: ");
        if (!int.TryParse(Console.ReadLine(), out int schoolId))
        {
            Console.WriteLine("无效的ID");
            return;
        }

        Console.Write("班级名称: ");
        var name = Console.ReadLine();

        var cls = new Class { Name = name, SchoolId = schoolId };
        var id = _dbHelper.AddClass(cls);

        Console.WriteLine($"班级添加成功，ID: {id}");
    }

    static void ListAllClasses()
    {
        Console.WriteLine("\n班级列表:");
        Console.WriteLine("ID\t名称\t所属学校");
        Console.WriteLine("----------------------------");

        var classes = _dbHelper.GetAllClassesWithSchool();
        foreach (var cls in classes)
        {
            Console.WriteLine($"{cls.Id}\t{cls.Name}\t{cls.School?.Name ?? "未知"}");
        }
    }
    #endregion

    #region 学生管理
    static void ManageStudents()
    {
        while (true)
        {
            Console.WriteLine("\n学生管理");
            Console.WriteLine("1. 添加学生");
            Console.WriteLine("2. 查看所有学生");
            Console.WriteLine("3. 更新学生信息");
            Console.WriteLine("4. 删除学生");
            Console.WriteLine("5. 返回主菜单");
            Console.Write("请选择操作: ");

            var choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    AddStudent();
                    break;
                case "2":
                    ListAllStudents();
                    break;
                case "3":
                    UpdateStudent();
                    break;
                case "4":
                    DeleteStudent();
                    break;
                case "5":
                    return;
                default:
                    Console.WriteLine("无效选项");
                    break;
            }
        }
    }

    static void AddStudent()
    {
        ListAllClasses();
        Console.Write("\n请输入所属班级ID: ");
        if (!int.TryParse(Console.ReadLine(), out int classId))
        {
            Console.WriteLine("无效的ID");
            return;
        }

        Console.Write("学生姓名: ");
        var name = Console.ReadLine();

        Console.Write("学生年龄: ");
        if (!int.TryParse(Console.ReadLine(), out int age))
        {
            Console.WriteLine("无效的年龄");
            return;
        }

        Console.Write("学生性别(M/F): ");
        var gender = Console.ReadLine();

        var student = new Student
        {
            Name = name,
            Age = age,
            Gender = gender,
            ClassId = classId
        };

        var id = _dbHelper.AddStudent(student);
        Console.WriteLine($"学生添加成功，ID: {id}");
    }
    #endregion

    #region 日志管理
    static void ViewLogs()
    {
        Console.WriteLine("\n操作日志");
        Console.WriteLine("ID\t操作\t实体类型\t实体ID\t操作时间\t操作者");
        Console.WriteLine("--------------------------------------------------");

        var logs = _dbHelper.GetAllLogs();
        foreach (var log in logs)
        {
            Console.WriteLine($"{log.Id}\t{log.Operation}\t{log.EntityType}\t{log.EntityId}\t{log.OperationTime}\t{log.Operator}");
        }
    }
    #endregion

    static void UpdateClass()
    {
        ListAllClasses();
        Console.Write("\n请输入要更新的班级ID: ");
        if (!int.TryParse(Console.ReadLine(), out int id))
        {
            Console.WriteLine("无效的ID");
            return;
        }

        var cls = _dbHelper.GetClassById(id);
        if (cls == null)
        {
            Console.WriteLine("未找到该班级");
            return;
        }

        Console.Write($"新名称(当前: {cls.Name}): ");
        var name = Console.ReadLine();
        if (!string.IsNullOrWhiteSpace(name))
        {
            cls.Name = name;
        }

        _dbHelper.UpdateClass(cls);
        Console.WriteLine("班级信息已更新");
    }

    static void DeleteClass()
    {
        ListAllClasses();
        Console.Write("\n请输入要删除的班级ID: ");
        if (!int.TryParse(Console.ReadLine(), out int id))
        {
            Console.WriteLine("无效的ID");
            return;
        }

        Console.Write("确认删除该班级？(y/n): ");
        if (Console.ReadLine().ToLower() == "y")
        {
            _dbHelper.DeleteClass(id);
            Console.WriteLine("班级已删除");
        }
    }

    static void ListAllStudents()
    {
        Console.WriteLine("\n学生列表:");
        Console.WriteLine("ID\t姓名\t年龄\t性别\t所属班级");
        Console.WriteLine("--------------------------------------------------");

        var students = _dbHelper.GetAllStudentsWithClass();
        foreach (var student in students)
        {
            Console.WriteLine($"{student.Id}\t{student.Name}\t{student.Age}\t{student.Gender}\t{student.Class?.Name ?? "未知"}");
        }
    }

    static void UpdateStudent()
    {
        ListAllStudents();
        Console.Write("\n请输入要更新的学生ID: ");
        if (!int.TryParse(Console.ReadLine(), out int id))
        {
            Console.WriteLine("无效的ID");
            return;
        }

        var student = _dbHelper.GetStudentById(id);
        if (student == null)
        {
            Console.WriteLine("未找到该学生");
            return;
        }

        Console.Write($"新姓名(当前: {student.Name}): ");
        var name = Console.ReadLine();
        if (!string.IsNullOrWhiteSpace(name))
        {
            student.Name = name;
        }

        Console.Write($"新年龄(当前: {student.Age}): ");
        if (int.TryParse(Console.ReadLine(), out int age))
        {
            student.Age = age;
        }

        Console.Write($"新性别(当前: {student.Gender}): ");
        var gender = Console.ReadLine();
        if (!string.IsNullOrWhiteSpace(gender))
        {
            student.Gender = gender;
        }

        ListAllClasses();
        Console.Write($"新班级ID(当前: {student.ClassId}): ");
        if (int.TryParse(Console.ReadLine(), out int classId))
        {
            student.ClassId = classId;
        }

        _dbHelper.UpdateStudent(student);
        Console.WriteLine("学生信息已更新");
    }

    static void DeleteStudent()
    {
        ListAllStudents();
        Console.Write("\n请输入要删除的学生ID: ");
        if (!int.TryParse(Console.ReadLine(), out int id))
        {
            Console.WriteLine("无效的ID");
            return;
        }

        Console.Write("确认删除该学生？(y/n): ");
        if (Console.ReadLine().ToLower() == "y")
        {
            _dbHelper.DeleteStudent(id);
            Console.WriteLine("学生已删除");
        }
    }
}